package com.foreveross;

import java.lang.reflect.InvocationTargetException;
import java.util.ArrayList;
import java.util.Collections;
import java.util.Comparator;
import java.util.Date;
import java.util.HashMap;
import java.util.List;
import java.util.Map;
import java.util.Properties;
import java.util.Set;

import org.quartz.Scheduler;

import com.foreveross.crawl.common.ChannelProperties;
import com.foreveross.crawl.common.em.TaskState;
import com.foreveross.crawl.common.exception.BusinessException;
import com.foreveross.crawl.common.exception.TaskStateException;
import com.foreveross.crawl.common.util.DateUtil;
import com.foreveross.crawl.dto.TaskModelDto;
import com.foreveross.crawl.vo.ChannelInfo;
import com.foreveross.crawl.vo.TaskGroup;
import com.foreveross.crawl.vo.TaskStatisticsBeta1;

/**
 * 对全局的Map缓存进行操作
 * @author luofangyi
 *
 */
public class GlobalMapOperatorBeta1 {

	/**
	 * 组(key)及渠道(value)Map
	 */
	public static Map<Long, List<Long>> groupAndChannel = new HashMap<Long, List<Long>>();
	
	/**
	 * 渠道(key)及任务(value)Map
	 */
	public static Map<Long, List<TaskModelDto>> channelAndTask = new HashMap<Long, List<TaskModelDto>>();
	
	/**
	 * 组信息MAP 组ID-组名称
	 */
	public static Map<Long, String> groupMap = new HashMap<Long, String>();
	
	/**
	 * 渠道信息MAP 渠道ID-渠道名称
	 */
	private static Map<Long, String> channelMap = new HashMap<Long, String>();
	
	private static Map<Long, ChannelInfo> channelDetailMap = new HashMap<Long, ChannelInfo>();
	
	private static Map<Object, Object> cache = new HashMap<Object, Object>();
	
	/**
	 * 任务ID及对应的Scheduler缓存Map
	 */
	private static Map<String, Scheduler> taskIdSchedulerMap = new HashMap<String, Scheduler>();
	
	/**
	 * 任务数量相关统计
	 */
	private static HashMap <Long, TaskStatisticsBeta1> taskStatisticsMap = new HashMap<Long, TaskStatisticsBeta1>();
	
	private static ChannelProperties cp;
	private static Properties channelProperties;
	
	static{
		cp = new ChannelProperties();
		cp.load();
		channelProperties = cp.getProp();
	}
	
	/**
	 * 增加组、渠道操作
	 * @param group
	 * @param channelList
	 */
	public static void putGroupAndChannel(Long group, List<Long> channelList){
		synchronized (GlobalMapOperatorBeta1.class) {
			//1. 在组-渠道Map中加入信息
			groupAndChannel.put(group, channelList);
			//2. 在渠道-任务Map中加入信息
			putChannelToChanelAndTask(channelList);
		}
	}
	
	/**
	 * 移除组操作
	 * @param group
	 */
	public static void removeGroup(Long group){
		synchronized (GlobalMapOperatorBeta1.class) {
			//1. 首先移除渠道-任务Map中的渠道信息
			removeChannelAndTask(groupAndChannel.get(group));
			//2. 移除组-渠道Map中的组信息
			groupAndChannel.remove(group);
			//3. 移除组信息Map中key为group的信息
			synchronized (groupMap) {
				groupMap.remove(group);
			}
		}
	}
	
	/**
	 * 添加渠道信息到 渠道-任务Map
	 * @param channelList
	 */
	public static void putChannelToChanelAndTask(List<Long> channelList){
		synchronized (GlobalMapOperatorBeta1.class) {
			for(Long channel : channelList){
				putChannelToChannelAndTaskOfSingle(channel);
			}
		}
	}
	
	/*public static void putTaskToChannelAndTask(Long channelId, TaskModelDto taskModelDto){
		synchronized (channelAndTask) {
			List<TaskModelDto> taskList = channelAndTask.get(channelId);
			if(taskList == null){
				taskList = new ArrayList<TaskModelDto>();
			}
			taskList.add(taskModelDto);
			channelAndTask.put(channelId, taskList);
		}
	}*/
	
	/**
	 * 添加单条渠道至 渠道-任务Map 
	 * @param channel
	 */
	public static void putChannelToChannelAndTaskOfSingle(Long channel){
		synchronized(GlobalMapOperatorBeta1.class){
			channelAndTask.put(channel, new ArrayList<TaskModelDto>());
		}
	}
	
	/**
	 * 移出单条 渠道-任务Map 中key为channel的渠道
	 * @param channel
	 */
	public static void removeChannelToChannelAndTaskOfSingle(Long channel){
		synchronized(GlobalMapOperatorBeta1.class){
			channelAndTask.remove(channel);
		}
	}
	
	/**
	 * 从 渠道-任务Map 中移除包含在channelList对象中渠道
	 * @param channelList
	 */
	private static void removeChannelAndTask(List<Long> channelList){
		synchronized (GlobalMapOperatorBeta1.class) {
			for(Long channel : channelList){
				removeChannelToChannelAndTaskOfSingle(channel);
			}
		}
	}
	
	/**
	 * 清空所有任务
	 */
	public static void clearAllTask(){
		synchronized (GlobalMapOperatorBeta1.class) {
			Set<Long> channelSet = getAllChannel();
			for(Long channel : channelSet){
				channelAndTask.put(channel, null);
			}
		}
	}
	
	/**
	 * 根据组增加渠道
	 * @param group
	 * @param channel
	 */
	public static void addChannel(Long group, Long channel){
		synchronized (GlobalMapOperatorBeta1.class) {
			List<Long> channelList = groupAndChannel.get(group);
			if(channelList == null){
				channelList = new ArrayList<Long>();
			}
			channelList.add(channel);
			groupAndChannel.put(group, channelList);
		}
	}
	
	/**
	 * 添加渠道下的任务信息
	 * @param channel
	 */
	public static void addTaskByChannel(Long channel, TaskModelDto task){
		synchronized (GlobalMapOperatorBeta1.class) {
			List<TaskModelDto> taskList = channelAndTask.get(channel);
			if(taskList == null){
				taskList = new ArrayList<TaskModelDto>();
			}
			taskList.add(task);
			channelAndTask.put(channel, taskList);
		}
	}
	
	public static int getTaskSizeByChannel(Long channelId){
		synchronized (GlobalMapOperatorBeta1.class) {
			List<TaskModelDto> taskList = channelAndTask.get(channelId);
			if(taskList == null){
				return 0;
			} else {
				return taskList.size();
			}
		}
	}
	
	/**
	 * 移除Map中指定渠道下的任务信息
	 * @param channelId
	 * @param taskId
	 */
	public static void removeTaskByChannelAndTask(Long channelId, String taskId){
		synchronized (GlobalMapOperatorBeta1.class) {
			List<TaskModelDto> taskList = channelAndTask.get(channelId);
			for(TaskModelDto dto : taskList){
				if(dto.getTaskId().equals(taskId) || dto.getTaskId() == taskId){
					taskList.remove(dto);
					break;
				}
			}
			channelAndTask.put(channelId, taskList);
		}
	}
	
	public static TaskModelDto getTaskByChannelAndTask(Long channelId, String taskId){
		List<TaskModelDto> taskList = channelAndTask.get(channelId);
		TaskModelDto retDto = null;
		for(TaskModelDto dto : taskList){
			if(dto.getTaskId().equals(taskId) || dto.getTaskId() == taskId){
				retDto = dto;
				break;
			}
		}
		return retDto;
	}
	
	/**
	 * 根据渠道及任务ID更新任务状态
	 * @param channel
	 * @param taskId
	 * @param state
	 * @throws TaskStateException 
	 */
	public static void updateTaskState(Long channel, String taskId, String state) throws TaskStateException{
		synchronized (GlobalMapOperatorBeta1.class) {
			List<TaskModelDto> taskList = channelAndTask.get(channel);
			if(taskList == null){
				throw new TaskStateException("更新的任务为空");
			}
			for(TaskModelDto dto : taskList){
//				Object taskObj = vo.getTask();
				if(dto == null){
					continue;
				}
				if(dto.getId() != null && dto.getId().equals(taskId)){
					dto.setTaskState(state);
				}
				/*if(taskObj instanceof TaskModel){
					TaskModel model = (TaskModel)taskObj;
					//提供的taskId与缓存中的任务ID相同
					if(model.getId() != null && model.getId().equals(taskId)){
						vo.setTaskState(state);
					}
				}*/
				//TODO  当新版任务中心开发好后可继续添加
//				else if(taskObj instanceof ){
//					
//				}
			}
			channelAndTask.put(channel, taskList);
		}
	}
	
	/**  
	 * @Description: 记录任务轨迹
	 * @param channel
	 * @param taskId
	 * @param tracks
	 * @author luofangyi
	 * @throws TaskStateException 
	 * @date 2014-7-15 上午10:39:32 
	 */ 
	public static void recordTracksOfTask(Long channel, String taskId, String tracks) throws TaskStateException{
		synchronized(GlobalMapOperatorBeta1.class) {
				List<TaskModelDto> taskList = channelAndTask.get(channel);
				if(taskList == null){
					throw new TaskStateException("更新的任务为空");
				}
				for(TaskModelDto dto : taskList){
					if(dto == null){
						continue;
					}
					if(dto.getId() != null && dto.getId().equals(taskId)){
						//更改了记录轨迹方式，故注释掉
						dto.getTracksList().add(tracks);
					}
					/*if(taskObj instanceof TaskModel){
						TaskModel model = (TaskModel)taskObj;
						//提供的taskId与缓存中的任务ID相同
						if(model.getId() != null && model.getId().equals(taskId)){
							vo.setTaskState(state);
						}
					}*/
					//TODO  当新版任务中心开发好后可继续添加
//					else if(taskObj instanceof ){
//						
//					}
				}
				channelAndTask.put(channel, taskList);
		}
	}
	
	/**  
	 * @Description: 根据任务ID遍历缓存，然后记录轨迹
	 * @param taskId
	 * @param tracks
	 * @throws TaskStateException
	 * @author luofangyi
	 * @date 2014-9-29 上午11:46:04 
	 */ 
	public static void recordTracksOfTask(String taskId, String tracks) throws TaskStateException{
		synchronized(GlobalMapOperatorBeta1.class) {
			Set<Long> keySet = channelAndTask.keySet();
			for(Long key : keySet){
				recordTracksOfTask(key, taskId, tracks);
			}
		}
	}
	
	/**  
	 * @Description: 根据适配器名称、任务ID进行轨迹的记录
	 * @param adatperClazz
	 * @param taskId
	 * @param tracks
	 * @throws TaskStateException
	 * @author luofangyi
	 * @date 2014-9-29 下午2:45:37 
	 */ 
	public static void recordTracksOfTask(String adatperClazz, String taskId, String tracks) throws TaskStateException{
//		Map<String, Object> map = GlobalMapOperator.getCache("channelGoup");
//		JSONObject channelJson = JSONObject.fromObject(map.get("channel"));
		
		Set<Object> channelSet = channelProperties.keySet();
		for(Object obj : channelSet){
			String name = (String) channelProperties.get(obj);
			if(adatperClazz.contains(name)){
				recordTracksOfTask((Long)obj, taskId, tracks);
				break;
			}
		}
	}
	
	/**  
	 * @Description: 更新任务开始时间
	 * @param channel
	 * @param taskId
	 * @throws TaskStateException
	 * @author luofangyi
	 * @date 2014-7-9 下午2:29:46 
	 */ 
	public static void updateTaskStartDate(Long channel, String taskId) throws TaskStateException{
		synchronized (GlobalMapOperatorBeta1.class) {
			List<TaskModelDto> taskList = channelAndTask.get(channel);
			if(taskList == null){
				throw new TaskStateException("更新的任务为空");
			}
			for(TaskModelDto dto : taskList){
//				Object taskObj = vo.getTask();
				if(dto == null){
					continue;
				}
				if(dto.getId() != null && dto.getId().equals(taskId)){
					dto.setTaskStartDate(DateUtil.formatDay(new Date(), "HH时mm分ss秒"));
				}
				/*if(taskObj instanceof TaskModel){
					TaskModel model = (TaskModel)taskObj;
					//提供的taskId与缓存中的任务ID相同
					if(model.getId() != null && model.getId().equals(taskId)){
						vo.setTaskState(state);
					}
				}*/
				//TODO  当新版任务中心开发好后可继续添加
//				else if(taskObj instanceof ){
//					
//				}
			}
			channelAndTask.put(channel, taskList);
		}
	}
	
	/**  
	 * @Description: 更新当前时间为任务的结束时间
	 * @param channel 渠道ID
	 * @param taskId 任务ID
	 * @author luofangyi
	 * @throws TaskStateException 
	 * @throws BusinessException 
	 * @date 2014-7-7 下午2:46:45 
	 */ 
	public static void updateTaskEndDateAndExcuteTime(Long channel, String taskId, long excuteTime, boolean isStateFinished) throws TaskStateException, BusinessException{
		synchronized (GlobalMapOperatorBeta1.class) {
			List<TaskModelDto> taskList = channelAndTask.get(channel);
			if(taskList == null){
				throw new TaskStateException("更新的任务为空");
			}
			for(TaskModelDto dto : taskList){
//				Object taskObj = vo.getTask();
				if(dto == null){
					continue;
				}
				if(dto.getId() != null && dto.getId().equals(taskId)){
					if(dto.getTaskExcuteTotal() == 0){
						dto.setTaskEndDate(DateUtil.formatDay(new Date(), "HH时mm分ss秒"));
						dto.setTaskExcuteTotal(excuteTime);
					}
					if(isStateFinished){
						dto.setTaskState(TaskState.FINISHED.getType());
						updateTaskSuccessNumber(dto.getChannelId());
					}
				}
				/*if(taskObj instanceof TaskModel){
					TaskModel model = (TaskModel)taskObj;
					//提供的taskId与缓存中的任务ID相同
					if(model.getId() != null && model.getId().equals(taskId)){
						vo.setTaskState(state);
					}
				}*/
				//TODO  当新版任务中心开发好后可继续添加
//				else if(taskObj instanceof ){
//					
//				}
			}
			channelAndTask.put(channel, taskList);
		}
	}
	
	public static void updateTaskEndDateAndExcuteTime(Long channel, String taskId, long excuteTime, String status) throws TaskStateException, BusinessException{
		synchronized (GlobalMapOperatorBeta1.class) {
			List<TaskModelDto> taskList = channelAndTask.get(channel);
			if(taskList == null){
				throw new TaskStateException("更新的任务为空");
			}
			for(TaskModelDto dto : taskList){
//				Object taskObj = vo.getTask();
				if(dto == null){
					continue;
				}
				if(dto.getId() != null && dto.getId().equals(taskId)){
					if(dto.getTaskExcuteTotal() == 0){
						dto.setTaskEndDate(DateUtil.formatDay(new Date(), "HH时mm分ss秒"));
						dto.setTaskExcuteTotal(excuteTime);
					}
					dto.setTaskState(status);
					updateTaskSuccessNumber(dto.getChannelId());
				}
				/*if(taskObj instanceof TaskModel){
					TaskModel model = (TaskModel)taskObj;
					//提供的taskId与缓存中的任务ID相同
					if(model.getId() != null && model.getId().equals(taskId)){
						vo.setTaskState(state);
					}
				}*/
				//TODO  当新版任务中心开发好后可继续添加
//				else if(taskObj instanceof ){
//					
//				}
			}
			channelAndTask.put(channel, taskList);
		}
	}
	
	/**  
	 * @Description: 更新当前时间为任务的结束时间,默认任务执行成功
	 * @param channel
	 * @param taskId
	 * @param excuteTime
	 * @throws TaskStateException
	 * @author luofangyi
	 * @throws BusinessException  
	 * @date 2014-8-27 下午5:42:19 
	 */ 
	public static void updateTaskEndDateAndExcuteTime(Long channel, String taskId, long excuteTime) throws TaskStateException, BusinessException{
		updateTaskEndDateAndExcuteTime(channel, taskId, excuteTime, true);
	}
	
	/**
	 * 返回所有渠道
	 * @return
	 */
	public static Set<Long> getAllChannel(){
		synchronized (GlobalMapOperatorBeta1.class) {
			return channelAndTask.keySet();
		}
	}
	
	public static List<TaskModelDto> getAllTask(){
		synchronized (GlobalMapOperatorBeta1.class) {
			List<TaskModelDto> retList = new ArrayList<TaskModelDto>();
			Set<Long> channelSet = getAllChannel();
			for(Long channelId : channelSet){
				List<TaskModelDto> list = channelAndTask.get(channelId);
				if(list != null){
					retList.addAll(list);
				}
			}
			return retList;
		}
	}
	
	/**
	 * 根据分组返回任务
	 * @param group
	 * @return
	 */
	public static List<TaskModelDto> getTaskByGroup(Long group){
		synchronized (GlobalMapOperatorBeta1.class) {
			List<TaskModelDto> retList = new ArrayList<TaskModelDto>();
			List<Long> channelList = groupAndChannel.get(group);
			for(Long channel : channelList){
				if(channelAndTask.get(channel) != null)
					retList.addAll(channelAndTask.get(channel));
			}
			return retList;
		}
	}
	
	/**
	 * 根据条件获取任务  (渠道编号)
	 * @param channelId
	 * @return
	 */
	public static List<TaskModelDto> getTaskByCondtion(Long channelId){
		synchronized (GlobalMapOperatorBeta1.class) {
			return channelAndTask.get(channelId);
		}
	}
	
	/**  
	 * @Description: 根据条件获取任务  (渠道编号、状态)
	 * @param channelId
	 * @param state 状态
	 * @return
	 * @author luofangyi
	 * @date 2014-6-26 下午2:50:21 
	 */ 
	public static List<TaskModelDto> getTaskByCondtion(Long channelId, String state){
		synchronized (GlobalMapOperatorBeta1.class) {
			List<TaskModelDto> retList = new ArrayList<TaskModelDto>();
			List<TaskModelDto> unFilterList = channelAndTask.get(channelId);
			if(unFilterList == null){
				return null;
			}
			if(state.equals("ALL")){
				retList = unFilterList;
			} else {
				for(TaskModelDto dto : unFilterList){
					if(dto.getTaskState().equals(state)){
						retList.add(dto);
					}
				}
			}
			if(retList != null && retList.size() > 1){
				//按任务开始时间降序排列
				Collections.sort(retList, new Comparator<TaskModelDto>(){
					public int compare(TaskModelDto f1, TaskModelDto f2){
						return f2.getTaskStartDate().compareTo(f1.getTaskStartDate());
					}
				});
			}
			return retList;
		}
	}
	
	public static void putGroupMap(Long groupId, String groupName){
		synchronized (GlobalMapOperatorBeta1.class) {
			groupMap.put(groupId, groupName);
		}
	}
	
	public static void putChannelMap(Long channelId, String channelName){
		synchronized (GlobalMapOperatorBeta1.class) {
			channelMap.put(channelId, channelName);
		}
	}
	
	/**  
	 * @Description: 返回渠道信息
	 * @return
	 * @author luofangyi
	 * @date 2014-7-15 下午5:40:13 
	 */ 
	public static Map<Long, String> getChannelMap(){
		synchronized (GlobalMapOperatorBeta1.class) {
			return channelMap;
		}
	}
	
	public static List<TaskGroup> getTaskGroupForList(){
		synchronized (GlobalMapOperatorBeta1.class) {
			List<TaskGroup> groupList = new ArrayList<TaskGroup>();
			Set<Long> groupSet = groupMap.keySet();
			for(Long groupId : groupSet){
				String groupName = groupMap.get(groupId);
				if(groupName.contains("航空")){
					groupList.add(new TaskGroup(groupId, groupName, true));
				} else {
					groupList.add(new TaskGroup(groupId, groupName));
				}
			}
			return groupList;
		}
	}
	
	public static void printTaskInfo(){
		Set<Long> channelSet = getAllChannel();
		System.out.println("=========start=============");
		for(Long channel : channelSet){
			List<TaskModelDto> taskList = channelAndTask.get(channel);
			for(TaskModelDto vo : taskList){
				System.out.println(vo);
			}
		}
		System.out.println("==========end============");
	}
	
	@SuppressWarnings("unchecked")
	public static <T> T getCache(Object key) {
		synchronized (GlobalMapOperatorBeta1.class) {
			return (T) cache.get(key);
		}
	}
	
	public static void setCache(Object key, Object value) {
		synchronized (GlobalMapOperatorBeta1.class) {
			cache.put(key, value);
		}
	}
	
	public static List<TaskModelDto> getAllTask(String status){
		synchronized (GlobalMapOperatorBeta1.class) {
			List<TaskModelDto> retList = new ArrayList<TaskModelDto>();
			List<TaskModelDto> unFilterList = new ArrayList<TaskModelDto>();
			Set<Long> channelSet = channelAndTask.keySet();
			for (Long channelId : channelSet) {
				if(channelAndTask.get(channelId) != null){//渠道对应的任务不为null
					unFilterList.addAll(channelAndTask.get(channelId));
				}
			}
			if (status.equals("ALL")) {
				retList = unFilterList;
			} else {
				for (TaskModelDto dto : unFilterList) {
					if (dto.getTaskState().equals(status)) {
						retList.add(dto);
					}
				}
			}
			if(retList != null && retList.size() > 1){
				//按任务开始时间降序排列
				Collections.sort(retList, new Comparator<TaskModelDto>(){
					public int compare(TaskModelDto f1, TaskModelDto f2){
						return f2.getTaskStartDate().compareTo(f1.getTaskStartDate());
					}
				});
			}
			return retList;
		}
	}

	public static Scheduler getSchedulerByTaskId(String taskId) {
		synchronized (GlobalMapOperatorBeta1.class) {
			return taskIdSchedulerMap.get(taskId);
		}
	}
	
	public static List<Scheduler> getAllScheduler(){
		synchronized (GlobalMapOperatorBeta1.class) {
			List<Scheduler> retList = new ArrayList<Scheduler>();
			Set<String> taskSet = taskIdSchedulerMap.keySet();
			for(String taskId : taskSet){
				retList.add(taskIdSchedulerMap.get(taskId));
			}
			return retList;
		}
	}

	public static void putTaskIdSchedulerMap(String taskId, Scheduler scheduler) {
		synchronized (GlobalMapOperatorBeta1.class) {
			taskIdSchedulerMap.put(taskId, scheduler);
		}
	}
	
	/**  
	 * @Description: 增加任务总数
	 * @param channelId
	 * @author luofangyi
	 * @throws BusinessException 
	 * @date 2014-7-11 下午6:14:33 
	 */ 
	public static void addTaskNumber(Long channelId){
		synchronized (GlobalMapOperatorBeta1.class) {
			TaskStatisticsBeta1 taskStatistics = getTaskStatisticsBeta1ByChannelId(channelId);
			taskStatistics.addTotal();
			taskStatisticsMap.put(channelId, taskStatistics);
		}
	}
	
	public static void setTaskNumber(Long channelId, int number){
		synchronized (GlobalMapOperatorBeta1.class) {
			TaskStatisticsBeta1 taskStatistics = getTaskStatisticsBeta1ByChannelId(channelId);
			taskStatistics.setTaskTotal(number);
			taskStatisticsMap.put(channelId, taskStatistics);
		}
	}
	
	public static void putTaskStatisticsBeta1Info(Long channelId){
		synchronized (GlobalMapOperatorBeta1.class) {
			TaskStatisticsBeta1 taskStatistics = new TaskStatisticsBeta1();
			taskStatisticsMap.put(channelId, taskStatistics);
		}
	}
	
	public static void putTaskStatisticsBeta1Info(Long channelId, TaskStatisticsBeta1 taskStatistics){
		synchronized (GlobalMapOperatorBeta1.class) {
			taskStatisticsMap.put(channelId, taskStatistics);
		}
	}
	
	/**  
	 * @Description: 更新正在做的任务数
	 * @param channelId
	 * @author luofangyi
	 * @throws BusinessException 
	 * @date 2014-7-11 下午6:14:45 
	 */ 
	public static void updateTaskDoingNumber(Long channelId) throws BusinessException{
		synchronized (GlobalMapOperatorBeta1.class) {
			TaskStatisticsBeta1 taskStatistics = getTaskStatisticsBeta1ByChannelId(channelId);
			if(taskStatistics == null){
				throw new BusinessException("找不到您要更新的任务统计数据");
			}
			taskStatistics.addDoing();
			taskStatisticsMap.put(channelId, taskStatistics);
		}
	}
	
	/**  
	 * @Description: 增加正在做的任务数
	 * @param channelId
	 * @throws BusinessException
	 * @author luofangyi
	 * @date 2014-10-13 下午1:02:53 
	 */ 
	public static void addTaskDoingNumber(Long channelId) throws BusinessException{
		synchronized (GlobalMapOperatorBeta1.class) {
			updateTaskDoingNumber(channelId);
		}
	}
	
	/**  
	 * @Description: 减少正在做的任务数
	 * @param channelId
	 * @throws BusinessException
	 * @author luofangyi
	 * @date 2014-10-13 下午1:04:08 
	 */ 
	public static void decreaseTaskDoingNumber(Long channelId) throws BusinessException{
		synchronized (GlobalMapOperatorBeta1.class) {
			TaskStatisticsBeta1 taskStatistics = getTaskStatisticsBeta1ByChannelId(channelId);
			if(taskStatistics == null){
				throw new BusinessException("找不到您要更新的任务统计数据");
			}
			int current = taskStatistics.getTaskDoing();
			if(current > 1){
				taskStatistics.setTaskDoing(current - 1);
				taskStatisticsMap.put(channelId, taskStatistics);
			}
		}
	}
	
	public static void setTaskDoingNumber(Long channelId, int number) throws BusinessException{
		TaskStatisticsBeta1 taskStatistics = getTaskStatisticsBeta1ByChannelId(channelId);
		if(taskStatistics == null){
			throw new BusinessException("找不到您要更新的任务统计数据");
		}
		taskStatistics.setTaskDoing(number);
		taskStatisticsMap.put(channelId, taskStatistics);
	}
	
	/**  
	 * @Description: 更新成功的任务数
	 * @param channelId
	 * @author luofangyi
	 * @throws BusinessException 
	 * @throws NoSuchMethodException 
	 * @throws InvocationTargetException 
	 * @throws InstantiationException 
	 * @throws IllegalAccessException 
	 * @date 2014-7-11 下午6:15:23 
	 */ 
	public static void updateTaskSuccessNumber(Long channelId) throws BusinessException{
		synchronized (GlobalMapOperatorBeta1.class) {
			TaskStatisticsBeta1 taskStatistics = getTaskStatisticsBeta1ByChannelId(channelId);
			if(taskStatistics == null){
				throw new BusinessException("找不到您要更新的任务统计数据");
			}
			taskStatistics.addSuccess();
			taskStatisticsMap.put(channelId, taskStatistics);
		}
	}
	
	public static void setTaskSuccessNumber(Long channelId, int number) throws BusinessException{
		TaskStatisticsBeta1 taskStatistics = getTaskStatisticsBeta1ByChannelId(channelId);
		if(taskStatistics == null){
			throw new BusinessException("找不到您要更新的任务统计数据");
		}
		taskStatistics.setTaskSuccess(number);
		taskStatisticsMap.put(channelId, taskStatistics);
	}
	
	public static void updateTaskSuccessNumber(Long channelId, int number) throws BusinessException{
		synchronized (GlobalMapOperatorBeta1.class) {
			TaskStatisticsBeta1 taskStatistics = getTaskStatisticsBeta1ByChannelId(channelId);
			if(taskStatistics == null){
				throw new BusinessException("找不到您要更新的任务统计数据");
			}
			taskStatistics.addSuccess(number);
			taskStatisticsMap.put(channelId, taskStatistics);
		}
	}
	
	/**  
	 * @Description: 更新异常的任务数
	 * @param channelId
	 * @author luofangyi
	 * @throws BusinessException 
	 * @date 2014-7-11 下午6:15:59 
	 */
	public static void updateTaskExceptionNumber(Long channelId) throws BusinessException{
		synchronized (GlobalMapOperatorBeta1.class) {
			TaskStatisticsBeta1 taskStatistics = getTaskStatisticsBeta1ByChannelId(channelId);
			if(taskStatistics == null){
				throw new BusinessException("找不到您要更新的任务统计数据");
			}
			taskStatistics.addFail();
			taskStatisticsMap.put(channelId, taskStatistics);
		}
	}
	
	public static void updateTaskExceptionNumber(Long channelId, int number) throws BusinessException{
		synchronized (GlobalMapOperatorBeta1.class) {
			TaskStatisticsBeta1 taskStatistics = getTaskStatisticsBeta1ByChannelId(channelId);
			if(taskStatistics == null){
				throw new BusinessException("找不到您要更新的任务统计数据");
			}
			taskStatistics.addFail(number);
			taskStatisticsMap.put(channelId, taskStatistics);
		}
	}
	
	public static void setTaskExceptionNumber(Long channelId, int number) throws BusinessException{
		TaskStatisticsBeta1 taskStatistics = getTaskStatisticsBeta1ByChannelId(channelId);
		if(taskStatistics == null){
			throw new BusinessException("找不到您要更新的任务统计数据");
		}
		taskStatistics.setTaskException(number);
		taskStatisticsMap.put(channelId, taskStatistics);
	}
	
	/**  
	 * @Description: 根据渠道获取TaskStatisticsBeta1信息
	 * @param channelId
	 * @return
	 * @author luofangyi
	 * @date 2014-7-11 下午6:24:38 
	 */ 
	public static TaskStatisticsBeta1 getTaskStatisticsBeta1ByChannelId(Long channelId){
		synchronized (GlobalMapOperatorBeta1.class) {
			TaskStatisticsBeta1 taskStatistics = null;
			if (taskStatisticsMap.containsKey(channelId)) {
				taskStatistics = taskStatisticsMap.get(channelId);
			} else {
				taskStatistics = new TaskStatisticsBeta1();
			}
			return taskStatistics;
		}
	}
	
	/**  
	 * @Description: 获取所有TaskStatisticsBeta1统计数据
	 * @return
	 * @author luofangyi
	 * @date 2014-7-11 下午6:18:13 
	 */ 
	public static Map<Long, TaskStatisticsBeta1> getTaskStatisticsBeta1(){
		synchronized (GlobalMapOperatorBeta1.class) {
			return taskStatisticsMap;
		}
	}
	
	/**  
	 * @Description: 清空TaskStatisticsBeta1统计数据
	 * @author luofangyi
	 * @date 2014-7-11 下午6:19:32 
	 */ 
	public static void clearAllTaskStatisticsBeta1(){
		synchronized (GlobalMapOperatorBeta1.class) {
			for(Long channelId : taskStatisticsMap.keySet()){
				taskStatisticsMap.remove(channelId);
			}
		}
	}
	
	public static void putChannelDetail(Long channelId, ChannelInfo channelInfo){
		synchronized (GlobalMapOperatorBeta1.class) {
			channelDetailMap.put(channelId, channelInfo);
		}
	}
	
	public static ChannelInfo getChannelInfoById(Long channelId){
		synchronized (GlobalMapOperatorBeta1.class) {
			return channelDetailMap.get(channelId);
		}
	}
	
	public static Map<Long, ChannelInfo> getAllChannelDetail(){
		synchronized (GlobalMapOperatorBeta1.class) {
			return channelDetailMap;
		}
	}
}
